import { ref } from 'vue'
import { defineStore } from 'pinia'
import { loginApI } from '@/apis/user'
 
import {useCartStore} from "@/stores/cartStore.js"
import {mergeCartAPI,findNewCartListAPI} from '@/apis/cart.js'

export const useUserStore = defineStore('user', () => {
  let CartStore=useCartStore()
  //  state
  let userInfo =ref({})
  // action
  const getUserInfo =async(user)=>{
      const res= await loginApI(user)
      userInfo.value=res.result
      console.log(userInfo);
      // 合并购物车接口
     let array= CartStore.cartList.map(item=>{
        return {
         skuId:   item.skuId,
         selected:   item.selected,
         count:   item.count,
      }})
      if(!array){
        await mergeCartAPI(array)
      }
    let res1=  await  findNewCartListAPI()
    CartStore.cartList.value=res1.result  
    } 

  const clearUserInfo=()=>{
    userInfo.value={}
   
  }
  return { getUserInfo,userInfo,clearUserInfo}
},
{
  persist: true,
} 
)
